/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controlador;

import com.logica.Administrativa;
import com.logica.Comercial;
import com.modelo.Inmueble;
import com.modelo.Reserva;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author AlEjAnDrA
 */
@Controller
@SessionAttributes
public class ReservaControl {

    @Autowired
    private Comercial comercial;
    @Autowired
    private Administrativa administrativa;

    public Comercial getComercial() {
        return comercial;
    }

    public void setComercial(Comercial comercial) {
        this.comercial = comercial;
    }

    public Administrativa getAdministrativa() {
        return administrativa;
    }

    public void setAdministrativa(Administrativa administrativa) {
        this.administrativa = administrativa;
    }

    //Inicio Alejandra  
    @RequestMapping(value = "/consultarIngresos.htm"/*, method = RequestMethod.POST*/)
    public ModelAndView consultarIngresos(@RequestParam("idInmueble") String idInmueble/*, @RequestParam("inicio") String in, @RequestParam("fin") String fn*/) {

        ModelAndView mv = new ModelAndView("consultarIngresos");

        String []tmp = idInmueble.split("-");
        Inmueble inmueble = new Inmueble();
        int id = Integer.parseInt(tmp [0]);
        inmueble.setIdInmueble(id);

        List<Reserva> reservas = new ArrayList<Reserva>();
        String in = tmp[1];
        String fn = tmp[2];

        if (in != null && !in.equals("0")) {
            String[] i = in.split("/");
            String[] f = fn.split("/");

            Date inicio = new Date(Integer.parseInt(i[2]), Integer.parseInt(i[1]), Integer.parseInt(i[0]));
            Date fin = new Date(Integer.parseInt(f[2]), Integer.parseInt(f[1]), Integer.parseInt(f[0]));
            reservas = comercial.ingresosPorFecha(inmueble, inicio, fin);
        } else {
            reservas = comercial.ingresosPorInmueble(null, inmueble);
        }

        mv.addObject("reservas", reservas);

        return mv;
    }
}
